package com.jianyun.jyzs.utils;


import android.support.annotation.NonNull;

import com.j256.ormlite.dao.CloseableIterator;
import com.j256.ormlite.dao.CloseableWrappedIterable;
import com.j256.ormlite.dao.ForeignCollection;
import com.jianyun.jyzs.bean.CityModel;
import com.jianyun.jyzs.bean.ProvinceModel;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;


public class XmlParserHandler extends DefaultHandler {

	/**
	 * 存储所有的解析对象
	 */
	private List<ProvinceModel> provinceList = new ArrayList<ProvinceModel>();
	 	  
	public XmlParserHandler() {
		
	}

	public List<ProvinceModel> getDataList() {
		return provinceList;
	}

	@Override
	public void startDocument() throws SAXException {
		// 当读到第一个开始标签的时候，会触发这个方法
	}

	ProvinceModel provinceModel = new ProvinceModel();
	CityModel cityModel = new CityModel();
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		// 当遇到开始标记的时候，调用这个方法
		if (qName.equals("province")) {
			provinceModel = new ProvinceModel();
			provinceModel.setName(attributes.getValue(0));
			provinceModel.setCityList(new ArrayList<CityModel>());
		} else if (qName.equals("city")) {
			cityModel = new CityModel();
			cityModel.setName(attributes.getValue(0));
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		// 遇到结束标记的时候，会调用这个方法
		if (qName.equals("district")) {
//			cityModel.getDistrictList().add(districtModel);
        } else if (qName.equals("city")) {
        	provinceModel.getCityList().add(cityModel);
        } else if (qName.equals("province")) {
        	provinceList.add(provinceModel);
        }
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
	}

	class Flist<T> implements ForeignCollection {

		@Override
		public CloseableIterator iterator(int i) {
			return null;
		}

		@Override
		public CloseableIterator closeableIterator(int i) {
			return null;
		}

		@Override
		public CloseableIterator iteratorThrow() throws SQLException {
			return null;
		}

		@Override
		public CloseableIterator iteratorThrow(int i) throws SQLException {
			return null;
		}

		@Override
		public CloseableWrappedIterable getWrappedIterable() {
			return null;
		}

		@Override
		public CloseableWrappedIterable getWrappedIterable(int i) {
			return null;
		}

		@Override
		public void closeLastIterator() throws SQLException {

		}

		@Override
		public boolean isEager() {
			return false;
		}

		@Override
		public int update(Object o) throws SQLException {
			return 0;
		}

		@Override
		public int updateAll() throws SQLException {
			return 0;
		}

		@Override
		public int refresh(Object o) throws SQLException {
			return 0;
		}

		@Override
		public int refreshAll() throws SQLException {
			return 0;
		}

		@Override
		public int refreshCollection() throws SQLException {
			return 0;
		}

		@Override
		public boolean add(Object o) {
			return false;
		}

		@Override
		public boolean addAll(@NonNull Collection collection) {
			return false;
		}

		@Override
		public void clear() {

		}

		@Override
		public boolean contains(Object o) {
			return false;
		}

		@Override
		public boolean isEmpty() {
			return false;
		}

		@NonNull
		@Override
		public Iterator iterator() {
			return null;
		}

		@Override
		public boolean remove(Object o) {
			return false;
		}

		@Override
		public int size() {
			return 0;
		}

		@NonNull
		@Override
		public Object[] toArray() {
			return new Object[0];
		}

		@NonNull
		@Override
		public Object[] toArray(@NonNull Object[] objects) {
			return new Object[0];
		}

		@Override
		public boolean retainAll(@NonNull Collection collection) {
			return false;
		}

		@Override
		public boolean removeAll(@NonNull Collection collection) {
			return false;
		}

		@Override
		public boolean containsAll(@NonNull Collection collection) {
			return false;
		}

		@Override
		public CloseableIterator closeableIterator() {
			return null;
		}
	}

}
